//
//  TBStatusViewModel.swift
//  新浪微博
//
//  Created by bin on 16/2/2.
//  Copyright © 2016年 XMG. All rights reserved.
//

import UIKit
import SDWebImage

class TBStatusViewModel {

    var status:TBStatus
    init(status:TBStatus){
        self.status = status
        //处理sourceText
        let str = status.source! as NSString
        let begin = str.rangeOfString(">").location + 1
        let end = str.rangeOfString("<", options: NSStringCompareOptions.BackwardsSearch).location
        sourceText = str.substringWithRange(NSMakeRange(begin, end - begin))
        //处理imageURLs
        var urls = [NSURL]()
        if let picurls = status.pic_urls{
            for dict in picurls{
                let urlStr = dict["thumbnail_pic"] as! String
                urls.append(NSURL(string: urlStr)!)
            }
        }
        
        if let picurls = status.retweeted_status?.pic_urls{
            var urls = [NSURL]()
            
            for dict in picurls{
                let urlStr = dict["thumbnail_pic"] as! String
                urls.append(NSURL(string: urlStr)!)
            }
        }
        imageURLs = urls
        
    }
    
    var createdTime:String{
        guard let create = status.created_at else{
            TBLog("created_at==nil")
            return ""
        }
        let date = NSDate.dataWithString(create)
        return date!.desc
    }
    
    
    lazy var rect:(imageRect: CGSize, colleRect: CGSize) = {
        /*
        没有配图: 不显示CGSizeZero
        一张配图: 图片的尺寸就是配图和配图容器的尺寸
        四张配图: 田字格
        其他张配图: 九宫格
        */
        // 1.获取配图个数
        guard let count = self.imageURLs?.count else{
            print("pic_urls == nil")
            return (CGSizeZero, CGSizeZero)
        }
        
        
        // 2.判断有没有配图
        if count == 0
        {
            return (CGSizeZero, CGSizeZero)
        }
        
        // 3.判断是否是一张配图
        if count == 1
        {
            let urlStr = self.imageURLs!.last!.absoluteString
            // 加载已经下载好得图片
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(urlStr)
            
            // 获取图片的size
            return (image.size, image.size)
        }
        
        let imageWidth:CGFloat = 90
        let imageHeight = imageWidth
        let imageMargin: CGFloat = 10
        // 4.判断是否是4张配图
        if count == 4
        {
            let col:CGFloat = 2
            // 计算宽度  宽度 = 列数 * 图片宽度+ (列数 - 1) * 间隙
            let width = col * imageWidth + (col - 1) * imageMargin
            // 计算高度
            let height = width
            return (CGSize(width: 90, height: 90), CGSize(width: width, height: height))
        }
        
        // 5.其它张配图  九宫格
        let col: CGFloat = 3
        let row  =  (count - 1) / 3 + 1
        let width = col * imageWidth + (col - 1) * imageMargin
        let height = CGFloat(row) * imageHeight + CGFloat(row - 1) * imageMargin
        return (CGSize(width: 90, height: 90), CGSize(width: width, height: height))
    }()

    
    var imageURLs: [NSURL]?
    //    var original_pic: [String]?
    /// 字符串型的微博ID
    
    /// 来源字符串
    var sourceText: String = ""
    
}
